import { Message } from 'element-ui'
import sjzdService from '@/api/sjzd'
import TreeUtil from '@/utils/tree'
export const getFldmList = () => {
  sjzdService
    .queryAllfldm()
    .then(res => {
      const fldmObj = groupBy(res, 'lbbm')
      // 缓存到sessionStorage
      window.sessionStorage.setItem('fldmList', JSON.stringify(fldmObj))
    })
    .catch(err => {
      !err.isError && Message.warning(err.message)
    })
}
function groupBy (array, key) {
  const groups = {}
  // 先按照key进行分组
  array.forEach(item => {
    const group = item[key]
    groups[group] = groups[group] || []
    groups[group].push(item)
  })
  // 组装树形结构
  Object.keys(groups).forEach(group => {
    const data = TreeUtil.getTreeData(groups[group], 'dm', 'fdm')
    groups[group] = data
  })
  return groups
}
